#ifndef UDTCLIENT_H
#define UDTCLIENT_H
#include <Poco/Net/SocketAddress.h>
#include <udt.h>
#include <list>
#include <Poco/ActiveMethod.h>
#include <Poco/Tuple.h>
#include <Poco/Net/DatagramSocket.h>

class UDTClient
{
public:
	UDTClient(Poco::UInt64 aSession , const Poco::Net::SocketAddress& aUdtServerAddr);
	UDTClient(Poco::UInt64 aSession , const Poco::Net::SocketAddress& aUdtServerAddr , const Poco::Net::SocketAddress& aUDPServerAddr);

	virtual ~UDTClient(){}
	typedef Poco::Tuple<UDTSOCKET , Poco::Net::SocketAddress> TConnectParam;
	typedef Poco::Tuple<bool , UDTSOCKET> TConnectResult;
public:
	// can be use QT signal

	bool ConnectWithPeerImp();

	bool IsConnected();

	UDTSOCKET GetPeerSocket() const {return iPeerSocket ;};
	Poco::ActiveMethod<TConnectResult , TConnectParam , UDTClient> TryConnect;

	bool UDPHolePuching();
        bool IsP2P()const {return iIsP2P;}
private:
	bool ConnectToServer(UDTSOCKET);
	void GetPeerAddr(UDTSOCKET , Poco::Net::SocketAddress& aPeerAddr);
	void GetPeerAddress(UDTSOCKET , std::string& aHost , std::string& aPort);
	void GetPeerAddress(UDTSOCKET , std::string& aHostPort);
	bool GetPeerAddress(UDTSOCKET , std::list<Poco::Net::SocketAddress>& aPeerAddrList );

	//bool CreateConnectionToFriend(UDTSOCKET , UDTSOCKET , const std::string& aPeerHostPort);
	bool CreateConnectionToFriend(UDTSOCKET aConnectedToServerSocket, UDTSOCKET aFuturePeerSocket, const std::list<Poco::Net::SocketAddress>& aPeerAddrList);

	TConnectResult TryConnectImp(const TConnectParam& );

public:
	// Send Data with exactly size
	static bool SendData(UDTSOCKET aSocket,const char* aData, int aSize);
	static bool SendData(UDTSOCKET aSocket , const std::string& aData);

	// receive Data with exactly size
	static bool ReceiveData(UDTSOCKET aSocket , char* aData, int aSize);
	static bool ReceiveData(UDTSOCKET aSocket , std::string& aData); // first 2 byte is length of string

#ifdef _USE_QT
signals:
	void ConnectedWithPeer();
#endif
protected:

private:
	Poco::UInt64 iSessionID;
	UDTSOCKET iPeerSocket;
	Poco::Net::SocketAddress iUdtServerAddr;
	Poco::Net::SocketAddress iUDPServerAddr;

	Poco::Net::DatagramSocket iUdpSocket;
        bool iIsP2P;

};

#endif // UDTCLIENT_H
